home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / CLASSSRC.PAK / TIMER.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  3KB  |  112 lines

  1. //----------------------------------------------------------------------------
  2. // Borland Class Library
  3. // Copyright (c) 1991, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   5.8  $
  6. //
  7. // TTimer class implementation for DOS and Win16
  8. //----------------------------------------------------------------------------
  9. #include <classlib/pch.h>
  10. #include <classlib/defs.h>
  11. #include <tchar.h>
  12.  
  13. #if defined(BI_PLAT_DOS) || defined(BI_PLAT_WIN16)
  14.  
  15. #define BUILDBIDSTIMER
  16. #include <classlib/timer.h>
  17. #include <dos.h>
  18.  
  19. const unsigned long far * const dosTime =
  20.     (const unsigned long far * const)MK_FP( 0x40, 0x6C );
  21.  
  22. unsigned TTimer::Adjust = Calibrate();
  23.  
  24. TTimer::TTimer() : Time_(0), Running(0)
  25. {
  26. }
  27.  
  28. void TTimer::Start()
  29. {
  30.     if( !Running )
  31.         {
  32.         outportb( 0x43, 0x34 );
  33.         __emit__( 0xEB, 0x00 );
  34. //        asm jmp __1;
  35. //    __1:
  36.         outportb( 0x40, 0 );
  37.         __emit__( 0xEB, 0x00 );
  38. //        asm jmp __2;
  39. //    __2:
  40.         outportb( 0x40, 0 );
  41.         StartTime.DosCount = *dosTime;
  42.         StartTime.TimerCount = 0;
  43.         Running = 1;
  44.         }
  45. }
  46.  
  47. void TTimer::Stop()
  48. {
  49.     outportb( 0x43, 0 );
  50.     _TUCHAR temp = inportb( 0x40 );
  51.  
  52.     TIME stopTime;
  53.     stopTime.TimerCount = (inportb( 0x40 ) << 8) + temp;
  54.     stopTime.DosCount = *dosTime;
  55.  
  56.     TIME elapsedTime;
  57.     elapsedTime.DosCount = stopTime.DosCount - StartTime.DosCount;
  58.     elapsedTime.TimerCount = -( stopTime.TimerCount - Adjust );
  59.  
  60.     const double fudge = 83810.0/100000.0;
  61.     Time_ += ((elapsedTime.DosCount << 16) + elapsedTime.TimerCount)*fudge;
  62.  
  63.     Running = 0;
  64.  
  65. }
  66.  
  67. void TTimer::Reset()
  68. {
  69.     Time_ = 0;
  70.     if( Running )
  71.         Start();
  72. }
  73.  
  74. unsigned TTimer::Calibrate()
  75. {
  76.     Adjust = 0;
  77.     unsigned long sum = 0;
  78.     TTimer w;
  79.     for( int i = 0; i < 100; i++ )
  80.         {
  81.         w.Start();
  82.         w.Stop();
  83.         sum += w.Time();
  84.         w.Reset();
  85.         }
  86.     return (unsigned)((sum+5)/100);
  87. }
  88.  
  89. #if defined( TEST_TIMER )
  90. #include <iostream.h>
  91. #include <stdio.h>
  92.  
  93. int _tmain( void )
  94. {
  95.     delay( 0 );
  96.     cout << "Resolution: " << Timer::Resolution() << endl;
  97.     TTimer w;
  98.     for( unsigned del = 0; del < 10; del++ )
  99.         {
  100.         unsigned d1 = del*100;
  101.         w.Start();
  102.         delay( d1 );
  103.         w.Stop();
  104.         _tprintf( "%4u ms., actual time = %6f seconds.\n", d1, w.Time() );
  105.         w.Reset();
  106.         }
  107.     return 0;
  108. }
  109. #endif
  110.  
  111. #endif  //#if !defined(BI_PLAT_DOS) && !defined(BI_PLAT_WIN16)
  112.